import {
  Column,
  Entity,
  Index,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  Tree,
  TreeChildren,
  TreeParent,
} from 'typeorm';
import { CommonEntity, CommonStatus } from '~/common/entity/common.entity';
import { TenantEntity } from '~/modules/tenant/tenant.entity';
import { ArticleContentEntity } from '../article-content/article-content.entity';

@Entity({ name: 'ah_article_category' })
@Index(['id'])
@Tree('materialized-path')
export class ArticleCategoryEntity extends CommonEntity {
  @Column({ type: 'varchar', length: 255, comment: '分类名称' })
  name: string;

  @Column({ type: 'varchar', length: 100, comment: '分类编码', nullable: true })
  code: string;

  @Column({ type: 'text', comment: '分类描述', nullable: true })
  description: string;

  @Column({ type: 'tinyint', width: 1, comment: '状态', default: CommonStatus.YES })
  status: CommonStatus;

  @Column({ type: 'int', comment: '排序值', name: 'sort_order', default: 0 })
  sortOrder: number;

  @Column({
    type: 'tinyint',
    width: 1,
    comment: '是否已删除',
    default: CommonStatus.NO,
    name: 'is_deleted',
  })
  isDeleted: CommonStatus;

  @TreeParent()
  parent?: ArticleCategoryEntity;

  @TreeChildren({ cascade: true })
  children: ArticleCategoryEntity[];

  @ManyToOne(() => TenantEntity, (tenant) => tenant.articleCategories, {nullable: true})
  @JoinColumn({ name: 'tenant_id' })
  tenant?: TenantEntity;

  // @ManyToMany(() => ArticleContentEntity, (articleContent) => articleContent.categories)
  // @JoinTable({
  //   name: 'ah_article_category_content',
  //   joinColumn: { name: 'category_id' },
  //   inverseJoinColumn: { name: 'content_id' },
  // })
  // contents: ArticleContentEntity[];
}
